﻿using System;
using System.IO;
using Cloud.BaseCode;
using Library;
using Library.Cloud;
using Library.Util;

namespace Cloud.Cloud
{
    public partial class KullaniciAyarlari : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;

            var wallpapers = Directory.GetFiles(Server.MapPath("/Images/Backgrounds/"), "*.jpg");
            _wallpaperList.Text = "<table>";
            for (var i = 0; i < wallpapers.Length; i++)
            {
                if (i%4 == 0)
                    _wallpaperList.Text += "<tr>";

                _wallpaperList.Text += "<td><img src=\"/Images/Backgrounds/" + FileUtil.GetFileName(wallpapers[i]) + "\" onclick=\"SetWallpaper('" + FileUtil.GetFileName(wallpapers[i]) + "');\" /></td>";

                if (i%4 == 3)
                    _wallpaperList.Text += "</tr>";
            }
            _wallpaperList.Text += "</table>";
        }

        protected void SaveWallpaper(object sender, EventArgs e)
        {
            var wallpaper = GetQueryString("Wallpaper");

            if (Cast.IsNull(wallpaper))
                throw new Exception("Arkaplan resmi seçilmelidir.");

            CloudDao().Business().SetKullaniciAyar(Kullanici().FirmaKullaniciId.ToString(), CloudConst.KullaniciAyarlar.Wallpaper, wallpaper);
            Commit();

            AjaxInfo("Arkaplan resminiz kaydedildi.");
        }        
    }
}